from rest_framework import serializers

from common.models import College, Teacher, Class, Student, Lesson


class TeacherSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Teacher
        exclude = ('cls', )


class CollegeSerializer(serializers.ModelSerializer):
    teachers = serializers.SerializerMethodField()

    @staticmethod
    def get_teachers(college):
        queryset = Teacher.objects.filter(college__collid=college.collid)
        return TeacherSimpleSerializer(queryset, many=True).data

    class Meta:
        model = College
        fields = '__all__'


class CollegeSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = College
        fields = '__all__'


class StudentSimpleSerializer(serializers.ModelSerializer):

    class Meta:
        model = Student
        fields = ('stuid', 'name', 'status')


class LessonSerializer(serializers.ModelSerializer):
    class Meta:
        model = Lesson
        exclude = ('teacher', )


class StudentSerializer(serializers.ModelSerializer):
    lesson = serializers.SerializerMethodField()

    @staticmethod
    def get_lesson(student):
        return LessonSerializer(student.lesson, many=True).data

    class Meta:
        model = Student
        fields = '__all__'


class ClassSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Class
        fields = '__all__'


class ClassSerializer(serializers.ModelSerializer):
    student = serializers.SerializerMethodField()

    @staticmethod
    def get_student(cls):
        queryset = Student.objects.filter(cls__clsid=cls.clsid)
        return StudentSimpleSerializer(queryset, many=True).data

    class Meta:
        model = Class
        fields = '__all__'


class TeacherSerializer(serializers.ModelSerializer):

    class Meta:
        model = Teacher
        fields = '__all__'


